import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:news/com.comm/contants/config.dart';
import 'package:news/com.comm/utils/logutils.dart';

// var api = 'https://api-dev.ligeit.com/graphql';
// var postapi = 'https://api-dev.ligeit.com/';

// 连接婷婷测试机，暂时注释掉上面的
// var api = 'http://192.168.1.242:9999/graphql';
// var postapi = 'http://192.168.1.242:9999/';

// 连接旺旺测试机，暂时注释掉上面的
// var api = 'http://192.168.1.98:9999/graphql';
// var postapi = 'http://192.168.1.98:9999';
Utf8Decoder utf8decoder = Utf8Decoder();

Future<Map<String, dynamic>?> post(
    {required String apiArea, Map<String, dynamic>? variables}) async {
  String platform = 'ios';
  // if (Platform.isAndroid) {
  //   platform = 'android';
  // } else {
  //   platform = 'ios';
  // }

  // PackageInfo packageInfo = await PackageInfo.fromPlatform();
  var headers = {
    'Content-Type': 'application/json',
    "clientType": "app",
    "App-Platform": platform,
    "version": "1.0.0",
    "token":
        "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzkyMzY0OTUsInVzZXJuYW1lIjoieGlhb2xpbiJ9.NBUSFqSrj55Ut1m43Igyz-BnwJgmElqBH81hKcQ6AcU"
  };

  String mApi = baseApi + apiArea;

  var body = jsonEncode(variables);
  TblLogUtil.d(
      "http===api=${mApi.toString()}||headers=${headers.toString()}||body=${body.toString()}");
  try {
    var url = Uri.parse(mApi);
    var response = await http.post(url, headers: headers, body: body);
    TblLogUtil.d(
        "get--response.body" + utf8decoder.convert(response.body.codeUnits));
    if (response.statusCode == 200) {
      String dataS = utf8decoder.convert(response.body.codeUnits);
      var data = jsonDecode(dataS);
      return data;
    } else {
      return null;
    }
  } on Exception catch (e) {
    LogUtil.v("网络异常，请检查网络连接！");
    throw e;
  }
}

Future<Map<String, dynamic>?> get({required String apiArea}) async {
  String platform = 'ios';
  // if (Platform.isAndroid) {
  //   platform = 'android';
  // } else {
  //   platform = 'ios';
  // }
  // PackageInfo packageInfo = await PackageInfo.fromPlatform();
  var headers = {
    'Content-Type': 'application/json',
    "clientType": "app",
    "App-Platform": platform,
    "token":
        "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzkyMzY0OTUsInVzZXJuYW1lIjoieGlhb2xpbiJ9.NBUSFqSrj55Ut1m43Igyz-BnwJgmElqBH81hKcQ6AcU",
    "version": "1.0.0"
  };

  String mApi = "";
  mApi = baseApi + apiArea;
  TblLogUtil.d(
      "http==========api=${mApi.toString()}||headers=${headers.toString()}");
  // try {
  var url = Uri.parse(mApi);
  var response = await http.get(url, headers: headers);
  TblLogUtil.d("get--response.body:" + response.body);
  if (response.statusCode == 200) {
    String jsonString = response.body;
    // String fixedJsonString = jsonString.replaceAll("'", "\"");
    var data = jsonDecode(jsonString);
    return data;
  } else {
    return null;
  }
  // } on Exception catch (e) {
  //   throw e;
  // }
}
